Main
expand_more
subdirectory_arrow_right Registration
Products
expand_more
subdirectory_arrow_right Maisema.Pro
subdirectory_arrow_right eForms
subdirectory_arrow_right Youth Card
expand_more
subdirectory_arrow_right
subdirectory_arrow_right Hourlog
Services
expand_more
subdirectory_arrow_right Websites
subdirectory_arrow_right Webcasting
subdirectory_arrow_right Hosting
subdirectory_arrow_right eLearning
Projects (Global)
Wiki
expand_more
subdirectory_arrow_right Framework
expand_more
subdirectory_arrow_right
subdirectory_arrow_right
subdirectory_arrow_right
subdirectory_arrow_right
subdirectory_arrow_right
subdirectory_arrow_right iCMS 3.1
expand_more
subdirectory_arrow_right
subdirectory_arrow_right
EU Projects
expand_more
subdirectory_arrow_right Project partners
User Guides
expand_more
subdirectory_arrow_right iCMS Content Management System
expand_more
subdirectory_arrow_right
subdirectory_arrow_right
subdirectory_arrow_right
Contact
Innoventum Organisation Presentation

Innoventum.fi

Object relations

Object Relations

Methods 

  • AddRelation($object|[$objectid,$objecttype],[$relationstrength="weak"|"strong"],[REVERSE])
  • Attach() - Alias to Addrelation($object|[$objectid,$objecttype],"strong");
  • RemoveRelation($object|[$objectid,$objecttype])
  • RemoveRelations($objecttype)
  • LoadRelated($type,[REVERSE]) - Return a list of objects of $type related to the object
  • FindRelated($type,[REVERSE]) - Return an array of object ids of $type related to the object (REVERSE: array of object ids of $type the object is related to)
  • IsRelated($type,[REVERSE|BOTH]) - Check if relation(s) to given type of objects exist
  • GetRelationList() - Return a reference to the relationlistmanager object
  • On object lists:
    • LoadWithRelations(relationtype|object(list),order,[REVERSE|BOTH]]) - Load a list of objects having relations to objects of type relationtype or given object or list of objects  
    • LWPAWithRelations((array)properties,(array)attributes,array(relationtype|object(list),order,[REVERSE|BOTH]),limit,lolimit) - Load a list of objects having relations to objects of type relationtype or given object or list of objects AND properties and/or attributes as with LWPA method
  • On single objects:
    • IsRelated($id, $type) - check if object is related to given object

Relation strength (type): weak / strong
 - A strong relation subjects the target object to be deleted if the (relation's) owner object id deleted, whereas in a weak relation, only the relationship is removed. Note that if the target object is deleted, the relation's owner remains unaffected regardless of the strength of the relation, making the relation directional in a sense.

REVERSE ( define("REVERSE","REVERSE"); ): Add a reverse relation; substitutes owner / target fields and thus reverses the direction of the relation.

SAMPLES

$User->Attach($image)      (  User ->> Image )    $image->AddRelation($User,'strong',REVERSE)
same as: $image->AttachTo($User) 

$User->AddRelation($image)  (  User -> Image  )  $image->AddRelation($User,'weak',REVERSE)

$User->AddRelation($image,'strong',REVERSE)  ( User

$User->AddRelation($image,'weak','REVERSE)   (  UserAddRelation($User);

 

$User->LoadRelated('image')         Loads items with relation   User -> Image,   User->> Image

$User->LoadRelated('image',REVERSE)   Loads items with relation User

 

Order of results

You can optionally sort the order of the result list by giving the sorting field(s) as the last parameter, with the direction (ASC / DESC).
Notice: Direction is needed for the method to recognize parameter as sort order.

Example:  $resultlist = $object->LoadRelated($type,"name ASC");

Example 1: Attach an image to a news object 


$imageid=2;
$newsmanager=new NewsManager($dbc,$log,$newsid);
$newsmanager->AddRelation($imageid,"image");

Example 2: Save a comment and attach it to a blogentry 
(Delete comment if blogentry is deleted)

irequire("forms");
$postdata=new Forms();
$data=$postdata->GetObject("comment_");

$comment=new CommentManager($dbc,$log);
$comment->SafeInsert($data);
$comment->Save();

$blogentry=new BlogentryManager($dbc,$log,$blogentryid);
$blogentry->AddRelation($comment,"strong");
//Could be also written as:

//$blogentry->Attach($comment); 

Example 3, live demo of adding and removing relations @ :http://dev1.innoventum.fi/innoventum/tools/relationtest

Source:

irequire("bulletinpostmanager","icms_core");
/* Use a bulletinpost as the object to have some goodness */
$post=new BulletinPostManager($dbc,$log,1);

/* We may have linked some images to the bulletinpost, let's try to load them */
echo("Load related images");

/* Get a list of imagemanager objects maybe */
$images=$post->LoadRelated("image");

/* Get a list of ids for attached objects of type "image" maybe */
$imageids=$post->FindRelated("image");
if(is_array($imageids))
    {
    echo("Imploded array of related images (FindRelated): ".implode(",",$imageids)." 
");
    }
else echo("Array of related images turned out as ".gettype($imageids)." instead.
");
if(is_object($images))
    {

    if(!$images->IsEmpty())
        {
            /* Okay, there were linked images and we've got a list of them. Let's play. */
        do {
            
            /* List images */
            echo("Related image: ".$images->GetProperty("id")." imageurl: ".$images->GetImageUrl(500)."
");
            
            /* Remove the relation using the image object */
            echo("Now remove relation :".(($post->RemoveRelation($images->GetObject())===true)?'OK':'Failed')."
");
            // $post->RemoveRelation($images->GetProperty("id"),"image"); works just as well..
            
            }while($images->Next());
        }    
        
    }
else {
    echo("Fail, gots ".gettype($images)." instead of an object
");
    echo("Let's add a relation then.");
    /* Add a relation to an image. (defaults to a weak relation.) */
    $addids=array(2,30,53);
    foreach($addids as $id)
        {
        echo("AddRelation(".$id.",\"image\"):");
        if($post->AddRelation($id,"image"))
            echo(" .. done
");
        else echo ("failed.
");
        }
    }

No pages available.

DEBUG MODE | Route: /en/wiki/framework/object-relations/:colorMode/dark | Page: Object relations (ID: 51) | Common Name: object relations | Language: fi | Execution Time: 2.9884 s | Db Queries: 238 | Access Level: 10
Performance Breakdown (Total: 2.988s) ▼ Click to expand
paths-initialized: 0s (0s) Paths initialized
server-config-loaded: 0s (0s) Server config loaded
composer-loaded: 0.001s (0.001s) Composer autoloader loaded
autoloaders-registered: 0s (0.001s) Autoloaders registered
request-parsed: 0s (0.001s) Request data parsed
site-config-loaded: 0.001s (0.002s) Site configuration loaded
objectmodel-constants-loaded: 0s (0.002s) ObjectModel constants loaded
objectmodel-log-initialized: 0s (0.002s) Log initialized
objectmodel-database-initialized: 0.001s (0.002s) Database connections initialized
objectmodel-cache-initialized: 0s (0.003s) Cache initialized
objectmodel-acm-initialized: 0.003s (0.005s) Access controller initialized
objectmodel-strings-initialized: 0s (0.006s) Language strings initialized
objectmodel-auditlog-initialized: 0.032s (0.038s) Site and audit log initialized
objectmodel-globals-evaluated: 0s (0.038s) Evaluated globals initialized
objectmodel-session-initialized: 1.428s (1.466s) Session initialized
objectmodel-compat-registered: 0s (1.466s) ObjectModel compatibility functions registered
version-paths-loaded: 0s (1.466s) Version-specific paths loaded
request-sanitized: 0.352s (1.818s) Request data sanitized
route-created: 0.001s (1.818s) Route object created
bootstrap-complete: 0s (1.818s) Bootstrap complete
route-set-current: 0.448s (2.266s) Current route set
language-initialized: 0s (2.267s) Request language initialized
handle-route-start: 0s (2.267s) Starting page route handler
layout-creating: 0.012s (2.279s) Creating Layout instance
layout-created: 0.008s (2.287s) Layout instance created
layout-displaying: 0s (2.287s) Starting layout display()
construct-page-start: 0.085s (2.372s) Starting constructPage()
footer-rendering: 0.617s (2.988s) Footer template data prepared
Loading...